package com.blog.controller.admin;

import com.blog.dto.AddCategoryByArticleDTO;
import com.blog.dto.ArticleDTO;
import com.blog.dto.PageDTO;
import com.blog.result.Result;
import com.blog.service.ArticleService;
import com.blog.vo.ArticleVO;
import com.blog.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminController")
@RequestMapping("/admin/article")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/ArticleList")
    public Result<List<ArticleVO>> ArticleList() {
        log.info("根据用户ID查询所有文章");
        List<ArticleVO> list = articleService.articleList();
        return Result.success(list);
    }

    //改成分页查询
    @PostMapping("/PageArticle")
    public Result<List<ArticleVO>> PageArticle(@RequestBody PageDTO pageDTO) {
        log.info("分页查询文章：{}", pageDTO);
        List<ArticleVO> list = articleService.pageArticle(pageDTO);
        return Result.success(list);
    }

    @GetMapping("/totalArticleByUid")
    public Result totalArticleByUid() {
        log.info("分页查询文章总数.....");
        Long total = articleService.totalArticle();
        return Result.success(total);
    }

    @GetMapping("/ArticleByValue/{value}")
    public Result<List<ArticleVO>> ArticleByValue(@PathVariable("value") String value) {
        log.info("根据值模糊查询符合的文章：{}", value);
        List<ArticleVO> list = articleService.articleListByValue(value);
        return Result.success(list);
    }

    @GetMapping("/ArticleByID/{id}")
    public Result<ArticleVO> ArticleByID(@PathVariable("id") Long id) {
        log.info("根据文章ID查询文章：{}", id);
        ArticleVO articleVO = articleService.articleListById(id);
        return Result.success(articleVO);
    }

    @PostMapping("/AddArticle")
    public Result AddArticle(@RequestBody ArticleDTO articleDTO) {
        log.info("添加文章：{}", articleDTO);
        articleService.addArticle(articleDTO);
        return Result.success();
    }

    @PutMapping("/EditArticle")
    public Result EditArticle(@RequestBody ArticleDTO articleDTO) {
        log.info("修改文章：{}", articleDTO);
        articleService.EditArticle(articleDTO);
        return Result.success();
    }

    @DeleteMapping("/DeleteArticle/{id}")
    public Result DeleteArticle(@PathVariable("id") Long id) {
        articleService.deleteArticle(id);
        log.info("根据ID删除文章：{}", id);
        return Result.success();
    }

    @GetMapping("/articleByNoCategory")
    public Result<List<ArticleVO>> articleByNoCategory() {
        log.info("查询未分配类别的文章");
        List<ArticleVO> list = articleService.articleByNoCategory();
        return Result.success(list);
    }

    @GetMapping("/articleByCategory/{categoryId}")
    public Result<List<ArticleVO>> articleByCategory(@PathVariable("categoryId") Long categoryId) {
        log.info("根据类别ID:{} ==》查询已分配类别的文章", categoryId);
        List<ArticleVO> list = articleService.articleByCategory(categoryId);
        return Result.success(list);
    }


    @GetMapping("/getOneArticleByCategory/{articleId}")
    public Result<List<CategoryVO>> getOneArticleByCategory(@PathVariable("articleId") Long articleId) {
        log.info("根据文章ID:{} ==》查询已分配文章类别", articleId);
        List<CategoryVO> list = articleService.getOneArticleByCategory(articleId);
        return Result.success(list);
    }

    @PostMapping("/AddCategoryByArticle")
    public Result AddCategoryByArticle(@RequestBody AddCategoryByArticleDTO addCategoryByArticleDTO) {
        log.info("根据文章ID添加文章类别：{}", addCategoryByArticleDTO);
        articleService.AddCategoryByArticle(addCategoryByArticleDTO);
        return Result.success();
    }

    @GetMapping("/articleListByIsRecommend/{IsRecommend}")
    public Result<List<ArticleVO>> getArticleListByIsRecommend(@PathVariable("IsRecommend") Long IsRecommend){
        log.info("查看是否推荐文章列表....");
        List<ArticleVO> list = articleService.IsRecommendArticle(IsRecommend);
        return Result.success(list);
    }


    @GetMapping("/EditArticleRecommend/{articleId}/{IsRecommend}")
    public Result<List<ArticleVO>> EditArticleRecommend(
            @PathVariable("articleId") Long articleId,
            @PathVariable("IsRecommend") Long IsRecommend){
        log.info("修改是否推荐文章{}：==》状态：{}",articleId,IsRecommend);
        articleService.EditArticleRecommend(articleId,IsRecommend);
        return Result.success();
    }
}
